package online.westonline.com.westonline.ui.fragment;

import android.content.Intent;
import android.content.SearchRecentSuggestionsProvider;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.easy.west.westclient.Constants;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.event.ConversationRefreshEvent;
import cn.jpush.im.android.api.event.MessageEvent;
import cn.jpush.im.android.api.event.MessageReceiptStatusChangeEvent;
import cn.jpush.im.android.api.event.MessageRetractEvent;
import cn.jpush.im.android.api.event.OfflineMessageEvent;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.adapter.im.ChatListAdpter;
import online.westonline.com.westonline.adapter.im.HomeMessageAdpter;
import online.westonline.com.westonline.base.BaseFragment;
import online.westonline.com.westonline.event.CreateConvEvent;
import online.westonline.com.westonline.ui.ChatActivity;

/**
 * 会话fragment
 * Created by xiatao on 2018/7/31.
 */

public class MessageFragment extends BaseFragment {

    @BindView(R.id.recyclerView_recyc)
    RecyclerView recyclerViewRecyc;
    //漫游完成
    private int REFREH_COMPLTE=0x3213;
    //新消息
    private int NEW_MESSAGE=0x14214;
    //离线消息
    private int OFFLINE_MESSAGE=0x152551;
    //特殊消息
    private int RETRACT_MESSAGE=0x21421;
    //已读
    private int RECIPET_STUATS=0x52355;
    private HomeMessageAdpter messageAdpter;
    private List<Conversation> conversationList = new ArrayList<>();
    private Handler myhandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what==REFREH_COMPLTE)
            {
                Conversation conversation = (Conversation) msg.obj;

            }
            if (msg.what==NEW_MESSAGE)
            {
                cn.jpush.im.android.api.model.Message message = (cn.jpush.im.android.api.model.Message) msg.obj;

            }
            if (msg.what==RETRACT_MESSAGE)
            {
                Conversation conversation = (Conversation) msg.obj;
                messageAdpter.setNewTop(conversation);
            }
            if (msg.what==OFFLINE_MESSAGE)
            {
                Conversation conversation = (Conversation) msg.obj;

            }
            if (msg.what==RECIPET_STUATS)
            {
                Conversation conversation = (Conversation) msg.obj;

            }
            return true;
        }
    });
    @Override
    protected boolean setEventOpen() {
        return false;
    }

    @Override
    protected void initdata() {
        //注册jmessge的事件监听
        JMessageClient.registerEventReceiver(this);
        //从本地数据库获取
       conversationList = JMessageClient.getConversationList();
    }

    @Override
    protected void initview() {
        messageAdpter = new HomeMessageAdpter();
        //初始化recyclerview
        recyclerViewRecyc.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerViewRecyc.setAdapter(messageAdpter);
        messageAdpter.setConversationList(conversationList);
        messageAdpter.notifyDataSetChanged();
        messageAdpter.setClicktoChat(new HomeMessageAdpter.onClicktoChat() {
            @Override
            public void tochat(int pos, Conversation conversation) {
                //清除未读角标
                conversationList.remove(conversation);
                conversation.resetUnreadCount();
                conversation.setUnReadMessageCnt(0);
                conversationList.add(pos,conversation);
                messageAdpter.setConversationList(conversationList);
                messageAdpter.notifyItemChanged(pos,conversation);
                //用户实体
                UserInfo target_user = (UserInfo) conversation.getTargetInfo();
                //如果是单聊
                Bundle bundle = new Bundle();
                //单聊标记false
                bundle.putBoolean(Constants.MODE, false);
                //单聊id
                bundle.putString(Constants.SINGLE_ID, target_user.getUserName());
                //单聊要用的appkey
                bundle.putString(Constants.TARGET_APPKEY, target_user.getAppKey());
                startActivityWithBundle(ChatActivity.class,false,bundle);
            }
        });


    }

    @Override
    protected int setlayout() {
        return R.layout.fragment_message;
    }
    //---------------------------------------------及时聊天im里的内容------------------------------
    /***
     * 漫游消息接收,消息刷新
     * @param event
     */
    public void onEvent(ConversationRefreshEvent event)
    {
        Conversation conversation = event.getConversation();
        /*
        Message message = new Message();
        message.obj=conversation;
        message.what=REFREH_COMPLTE;
        myhandler.sendMessage(message);
        myhandler.obtainMessage();
        */
        messageAdpter.setNewTop(conversation);

    }

    /***
     * 离线消息接收
     * @param event
     */
    public void onEventMainThread(OfflineMessageEvent event)
    {
        Conversation conversation = event.getConversation();
        messageAdpter.setNewTop(conversation);
    }

    /**
     * 新消息get
     * @param event
     */
    public void onEventMainThread(MessageEvent event)
    {
        cn.jpush.im.android.api.model.Message eventMessage = event.getMessage();
        //根据messageid获取会话
        Conversation conversation = JMessageClient.getSingleConversation(eventMessage.getFromUser().getUserName());
        messageAdpter.setNewTop(conversation);
    }

    /**
     * 通知类型的消息
     * @param event
     */
    public void onEventMainThread(MessageRetractEvent event)
    {
       Conversation conversation = event.getConversation();
        messageAdpter.setNewTop(conversation);
    }

    /***
     * 消息已读
     * @param event
     */
    public void onEventMainThread(MessageReceiptStatusChangeEvent event)
    {
        Conversation conversation = event.getConversation();
        messageAdpter.setNewTop(conversation);

    }
//---------------------------自定义消息事件-------------------

    /***
     * 创建新的聊天
     * @param convEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void createNewConv(CreateConvEvent convEvent)
    {
        Conversation conversation = convEvent.getConversation();
        messageAdpter.setNewTop(conversation);
    }


}
